//
//  HLCustomNotificationCenter.m
//  YueYue
//
//  Created by hulu001 on 2023/6/20.
//

#import "HLCustomNotificationCenter.h"
#import "HLSystemRingTool.h"
#import "HLRtcCallKit.h"
#import "HLCallTool.h"

@interface HLCustomNotificationCenter ()<HLRtcCallKitDelegate, NIMChatManagerDelegate>

@property (nonatomic, copy) NSString *invitor;

@end

@implementation HLCustomNotificationCenter


+ (instancetype)registerCustomNotificationCanter
{
    static dispatch_once_t onceToken;
    static HLCustomNotificationCenter *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)dealloc
{
    [HLRtcCallKit.sharedInstance removeDelegate:self];
    [[NIMSDK sharedSDK].chatManager removeDelegate:self];
    [NSNotificationCenter.defaultCenter removeObserver:self name:HLScreenChangedNotification object:nil];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [HLRtcCallKit.sharedInstance addDelegate:self];
        [[NIMSDK sharedSDK].chatManager addDelegate:self];
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(revievedScreenChanged:) name:HLScreenChangedNotification object:nil];
    }
    return self;
}


#pragma mark - <NERtcCallKitDelegate>

- (void)onInvited:(NSString *)invitor userIDs:(nonnull NSArray<NSString *> *)userIDs isFromGroup:(BOOL)isFromGroup groupID:(nullable NSString *)groupID type:(HLRtcCallType)type
{
    
    if ([self filterInvite]) {
        return;
    }
    
    if (type == HLRtcCallTypeAudio){
        return;
    }
    
//    if([HLRunTime shareInstance].isNoDisturbing && type == NERtcCallTypeVideo){
//        return;
//    }
    
    [[HLSystemRingTool shareInstance] playVideoCalling];
    self.invitor = invitor;
    
    // 如果有弹窗,手动关闭
    UIViewController *vc = [QMUIHelper visibleViewController];
    if ([vc isKindOfClass:[UIAlertController class]]) {
        [vc dismissViewControllerAnimated:NO completion:nil];
    }
    
    
    /*
     * 审核期间 直接弹出全屏接听
     */
    HLUserInfoModel *fromUser = [[HLUserInfoModel alloc] init];
    fromUser.userId = invitor;
    if (type == HLRtcCallTypeVideo) {
        [[HLCallTool shareInstance] calledFormCallUser:fromUser OnVC:[QMUIHelper visibleViewController]];
    }
}

- (void)onUserCancel:(NSString *)userID
{
    [[HLSystemRingTool shareInstance] finishPlay];
}

/// 收到消息
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages;
{
//    if ([[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLMessageViewController")]) {
//        [[HLSystemRingTool shareInstance] playRevievedMessage];
//    }
    
    // 处理礼物特效
    for (NIMMessage *msg in messages) {
        if (msg.messageType == NIMMessageTypeCustom) {
            NIMCustomObject *object = (NIMCustomObject *)msg.messageObject;
            if ([object.attachment isKindOfClass:NSClassFromString(@"HLGiftAttachment")]) {
                if ([self canAlertGiftPreview]) {
                    // 如果在视频中，弹出特效
                }
            }
        }
    }
}

#pragma mark - <截屏>

- (void)revievedScreenChanged:(NSNotification *)notification
{
}

#pragma mark - <私有>

/// 控制礼物全凭动图特效弹出
- (BOOL)canAlertGiftPreview
{
    BOOL f1 = NO;[[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLCallViewController")];
    BOOL f2 = [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLUserViewController")];
    
    return f1 || f2;
}

- (BOOL)filterInvite
{
    return [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"VideoMatchVC")] || [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLCallViewController")] || [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLLiveViewController")];
}

@end
